<template>
  <div>
    <el-card class="cardbox">
      <pagetool>
        <template #before>
          <el-button type="primary" size="small" @click="add">添加房产</el-button>
        </template>
        <template #after>
          <!-- el-selcet的v-model对应的是el-option的value值 -->
          <el-select slot="prepend" v-model="categ" placeholder="全部分类">
            <el-option
              label="全部分类"
              value=""
            />
            <el-option
              v-for="item in categoreies"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            />
          </el-select>
          <el-select slot="prepend" v-model="agent" placeholder="全部经纪">
            <el-option
              label="全部经纪"
              value=""
            />
            <el-option
              v-for="item in agents"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
          <el-input v-model="title" placeholder="请输入内容" clearable />
          <el-button slot="append" @click="serarch">筛选</el-button>
        </template>
      </pagetool>
      <el-divider />
      <!-- 表格 -->
      <el-table border="" :data="tableData">
        <el-table-column
          label="封面"
          align="center"
          prop="image"
        >
          <template v-slot="scope">
            <img :src="scope.row.image" style="height:200px;width:150px">
          </template>
        </el-table-column>
        <el-table-column
          label="标题"
          align="center"
          prop="title"
        />
        <el-table-column
          label="标签"
          align="center"
          prop="tags"
        >
          <template v-slot="scope">
            <el-tag
              v-for="item in scope.row.tags"
              :key="item"
              type="success"
            >
              {{ item }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="地址"
          align="center"
          prop="address"
        />
        <el-table-column
          label="分类"
          align="center"
          prop="category.title"
        />
        <el-table-column
          label="经纪"
          align="center"
          prop="agent.name"
        />
        <el-table-column
          label="价格"
          align="center"
          prop="price"
        />
        <el-table-column
          label="介绍"
          prop="desc"
          align="center"
        />
        <el-table-column
          label="图片"
          prop="photos"
          align="center"
        >
          <template v-slot="scope">
            <img
              v-for="item in scope.row.photos"
              :key="item"
              :src="item"
              style="height:50px;width:50px"
            >
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
        >
          <template v-slot="scope">
            <el-button type="primary" size="small" @click="edit(scope.row.id)">编辑</el-button>
            <el-button type="danger" size="small" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        class="page"
        layout="prev, pager, next"
        :total="total"
        :page-size="limit"
        :current-page="page"
        @current-change="pageChange"
      />
    </el-card>
    <Dialog
      ref="form"
      :show-dialog="showDialog"
      @close="close"
      @reload="reload"
    />
  </div>
</template>

<script>
import { getPropertyList, getAgents, getcategories, getCount, delProperty, detailProperty } from '@/api/property'
import Dialog from './components/dialog.vue'

export default {
  components: {
    Dialog
  },
  data() {
    return {
      tableData: [],
      limit: 2,
      start: 0,
      total: 0,
      page: 1,
      showDialog: false,
      // 分类列表
      categoreies: [],
      // 经纪列表
      agents: [],
      agent: '',
      categ: '',
      title: ''
    }
  },
  created() {
    // 经纪和分类
    this.getAgent()
    this.getCategoreie()

    this.getList()
    this.getCount()
  },
  methods: {
    async getList() {
      const res = await getPropertyList({
        _limit: this.limit,
        _start: this.start,
        title_contains: this.title || null,
        agent: this.agent || null,
        category: this.categ || null
      })
      // console.log(res)
      this.tableData = res
    },
    // 数量
    async getCount() {
      // 与getList请求参数不一样
      const res = await getCount({
        title_contains: this.title || null,
        agent: this.agent || null,
        category: this.categ || null
      })
      // console.log(res)
      this.total = res
    },
    // 页码改变
    pageChange(page) {
      // console.log(page)
      this.start = (page - 1) * 2
      this.getList()
      // 改变页码后重置，方便请求使用
      this.start = 0
    },
    async getAgent() {
      const res = await getAgents()
      // console.log(res)
      this.agents = res
      this.$refs.form.agents = res
    },
    async getCategoreie() {
      const res = await getcategories()
      // console.log(res)
      this.categoreies = res
      this.$refs.form.categoreies = res
    },
    add() {
      this.showDialog = true
    },
    async edit(id) {
      this.showDialog = true
      const res = await detailProperty(id)
      // console.log(res)
      this.$refs.form.DialogForm = {
        ...res,
        agent: res.agent.id,
        category: res.category.id
      }
      // 等到上传组件渲染完后拿数据
      this.$nextTick(() => {
        if (res.image) {
          this.$refs.form.$refs.image.fileList = [{ url: res.image }]
        }
        // 多地址格式
        // [
        //   {url: xxx},
        //   {url: xxx},
        //   {url: xxx}
        // ]
        this.$refs.form.$refs.photo.fileList = res.photos.map(item => { return { url: item } })
      })
    },
    async del(id) {
      try {
        await this.$confirm('是否删除')
        await delProperty(id)
        // 删除最后一条数据加载上一页
        if (this.start > 1 && this.tableData.length === 1) {
          this.start -= 2
        }
        this.$message.success('删除成功')
        this.getList()
        this.getCount()
      } catch (error) {
        console.log(error)
      }
    },
    close() {
      this.showDialog = false
    },
    reload() {
      this.getList()
      this.getCount()
    },
    serarch() {
      // 页码高亮回到第一页
      this.page = 1
      this.getList()
      this.getCount()
    }
  }
}
</script>

<style scoped>
.cardbox{
  margin: 20px;
}
.page{
  text-align: center;
}
</style>
